//
//  DataSingleton.m
//  EveryEnglish
//
//  Created by user on 14-6-6.
//  Copyright (c) 2014年 mxy. All rights reserved.
//

#import "DataSingleton.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonHMAC.h>
#import "Common.h"
@implementation DataSingleton

DEFINE_SINGLETON_FOR_CLASS(DataSingleton)

- init {
    if ((self = [super init])) {
        
        self.phone = [self getLocalData:@"wTel"];
        self.password = [self getLocalData:@"password"];
        self.uToken = [self getLocalData:@"uToken"];
        self.name = [self getLocalData:@"wholesaler_name"];
        self.dizhi = [self getLocalData:@"address"];
        self.newdizhi = [self getLocalData:@"address1"];
        self.shi = [self getLocalData:@"city"];
        self.shichang = [self getLocalData:@"market"];
        self.fanwei = [self getLocalData:@"push_radius"];
        self.jiage = [self getLocalData:@"start_price"];
        self.juli = [self getLocalData:@"near_range"];
        self.juli2 = [self getLocalData:@"far_range"];
        self.yunfei = [self getLocalData:@"near_price"];
        self.dianshu = [self getLocalData:@"start_point"];
        self.zjyunfei = [self getLocalData:@"over_price"];
        self.latitude = [self getLocalData:@"addX"];
        self.longtitude = [self getLocalData:@"addY"];
        
        NSDictionary * dic=[self getLocalData:@"data"];

        //为空相当于实例化数据
        if (dic)
        {
            self.userDic= [NSMutableDictionary dictionaryWithDictionary:dic];
        }
        else
        {
            
        }
            NSLog(@"self.userDic=%@",self.userDic);
    
        }
    return self;
}


//进行数据持久化的保存，获得相应的目录路径
- (NSString *)dataFilePath:(NSString *)fileName
{
    
    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths1 objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:fileName];
}

//获取本地数据,读取存储的数据
- (id)getLocalData:(NSString * )key
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
       
}



//进行储存数据，保存数据
- (void)saveLocalData:(NSString *)key value:(id)value
{
  
    if ([value isEqual:[NSNull null] ] || !value) {
        return;
    }
    if ([value isKindOfClass:[NSDictionary class]]){
        
        NSDictionary *dict = value;
        NSMutableArray *keys = @[].mutableCopy;
        [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            
            if ([obj isEqual:[NSNull null]] || !obj ) {
                

               
                [keys addObject:key];
            }
        }];
        
        NSMutableDictionary *dict_m = [NSMutableDictionary dictionaryWithCapacity:0];
        
             dict_m = dict.mutableCopy;
        
        //遍历出所有数据，判断是否有null进行重新赋值..深度遍历
        
//         NSLog(@"hhhhhhhh  %@",dict_m);
        
            [dict_m removeObjectsForKeys:keys];
//         NSLog(@"hhhhhhhh2  %@",dict_m);
        
            [[NSUserDefaults standardUserDefaults] setObject:dict_m forKey:key];
            [[NSUserDefaults standardUserDefaults] synchronize];

 
        
        
        
    }
     else if ([value isKindOfClass:[NSString class]])
       {
    
    [[NSUserDefaults standardUserDefaults] setObject:value forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];


      }
    else
    {
    
        return;
    }
    
    
    
    
    
}


//移除存储的数据
- (void)removeLocalData:(NSString *) key {
    return [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}



//移除创建的文件.图片存储的地方
- (void)removeLocalFile:(NSString *) path {
    NSFileManager *defaultManager = [NSFileManager defaultManager];
    NSError *err;
    [defaultManager removeItemAtPath:path error:&err];

}


//获得当前的时间
- (NSString *) timeString {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //[formatter setDateFormat: @"yyyy-MM-dd-HH-mm-ss-zzz"];
    [formatter setDateFormat: @"yyMMddHHmmss"];
    return [formatter stringFromDate:[NSDate date]];
}

+ (NSString *) hmacSha1:(NSString*)key text:(NSString*)text
{
    const char *cKey  = [key cStringUsingEncoding:NSUTF8StringEncoding];
    const char *cData = [text cStringUsingEncoding:NSUTF8StringEncoding];
    
    uint8_t cHMAC[CC_SHA1_DIGEST_LENGTH];
    
    CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
    
    //NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:CC_SHA1_DIGEST_LENGTH];
    NSString *hash;
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", cHMAC[i]];
    hash = output;
    
    return hash;
}




@end
